<?php

		
		class messageAction extends userAction {
			
			protected function _initialize() {
				parent::_initialize();
			}
			
			//收件箱
			public function index(){
				import("ORG.Util.Page"); 
				isset($_GET["p"]) ? $p = $_GET["p"] : $p = 1;
				$dao = D("message"); 
				$messageCount = $dao->count("userid=".$this->userInfo['id']); 
				$listRows = 20; 
				$p = new Page($messageCount,$listRows); 
				$p->setConfig('header' ,L("message_page_title")); 
				$messageList =	$dao->findAll("userid=".$this->userInfo['id'],"*",'regtime desc',$p->firstRow.','.$p->listRows);
				$page  = $p->show();
				$this->assign("messageList",$messageList);
				$this->assign("page",$page);
				$this->display();
			}
			
			//发件箱
			public function sent(){
				import("ORG.Util.Page"); 
				isset($_GET["p"]) ? $p = $_GET["p"] : $p = 1;
				$dao = D("message"); 
				$messageCount = $dao->count("postid=".$this->userInfo['id']); 
				$listRows = 20; 
				$p = new Page($messageCount,$listRows); 
				$p->setConfig('header' ,L("message_page_title")); 
				$messageList =	$dao->findAll("postid=".$this->userInfo['id'],"*",'regtime desc',$p->firstRow.','.$p->listRows);
				$page  = $p->show();
				$this->assign("messageList",$messageList);
				$this->assign("page",$page);
				$this->display();
			}
			//写私信
			public function write(){
				isset($_GET["u"]) ? $u = $_GET["u"] : $u = 0;
				if(0==$u){
					$friendList = D("friends")->findAll('userid='.$this->userInfo['id']);
					$this->assign("friendlist",$friendList);
				}else{
					$this->assign("u",$u);
				}
				$this->display();
			}
			//保存私信
			public function save(){
				if( D("user")->find("id=".$_POST["userid"],"id" )) {
					$dao = D("message");
					$dao->create();
					$dao->postid = $this->userInfo['id'];
					$dao->regtime = time();
					if($dao->add()){
						$this->success(L("send_message_success"));
					}else{
						$this->error(L("sned_message_error"));
					}	
				}else{
					$this->error(L("no_user"));
				}
				
			}
			//回复私信
			public function revert(){
				$dao = D("message");
				$dao->create();
				$dao->postid = $this->userInfo['id'];
				$dao->regtime = time();
				if($dao->add()){
					$this->success(L("send_message_success"));
				}else{
					$this->error(L("send_message_error"));
				}	
			}
			//删除私信
			public function delete(){
				$id = safeRequest($_GET["id"]);
				$dao = D("message");
				if($dao->delete("id=".$id." and  userid=".$this->userInfo['id'])){
					$dao->delete("parentid=".$id);
					isset($_GET["ajax"]) ? print "success" : $this->success(L("delete_message_success")); 				
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("delete_message_error")); 	
				}
			}
			//读私信
			public function read(){
				$id = safeRequest($_GET["id"]);
				$dao = D("message");
				$record = $dao->find("userid=".$this->userInfo['id']." and id=".$id);
				if($record){
					if(0 == $record->isread){
						$dao->setField("isread",1,"id=".$id);
					}
					$dao->setField("isread",1,"parentid=".$id." and userid=".$this->userInfo['id']);
					$this->assign("message",$record);
					//获得回复
					$revert = $dao->findAll("parentid=".$id,"*","regtime asc");
					$this->assign("revert",$revert);
					$this->display();
				}else{
					$this->error(L("no_record"));
				}
			}
			
			public function hello(){
				$userid = safeRequest($_GET["u"]);
				$dao = D("hello");
				if($dao->find("userid=".$userid." and postid=".$this->userInfo['id'])){
					if(isset($_GET["ajax"])){
						echo "repeat";
					}else{
						$this->error(L("has_hello"));
					}
					return false;
				}
				$dao->userid = $userid;
				$dao->postid = $this->userInfo['id'];
				$dao->content = str_filter($_GET["content"]);
				$dao->regtime = time();
				if($dao->add()){
					if(isset($_GET["ajax"])){
						echo "success";
					}else{
						$this->success(L("say_hello_success"));
					}
				}else{
					if(isset($_GET["ajax"])){
						echo "error";
					}else{
						$this->error(L("say_hello_success"));
					}
				}
			}
			
			public function reverthello(){
				$userid = safeRequest($_GET["u"]);
				$dao = D("hello");
				if($dao->find("postid=".$userid." and userid=".$this->userInfo['id'])){
					$dao->delete("postid=".$userid);
					$dao->userid = $userid;
					$dao->postid = $this->userInfo['id'];
					$dao->content = str_filter($_GET["content"]);
					$dao->regtime = time();
					if($dao->add()){
						if(isset($_GET["ajax"])){
							echo "success";
						}else{
							$this->success(L("revert_hello_success"));
						}
					}else{
						if(isset($_GET["ajax"])){
							echo "error";
						}else{
							$this->error(L("revert_hello_error"));
						}
					}
				}else{
					if(isset($_GET["ajax"])){
							echo "error";
						}else{
							$this->error(L("no_record"));
						}
				}
			}
			
			public function ignorehello(){
				$userid = safeRequest($_GET["u"]);
				$dao = D("hello");
				if($dao->find("postid=".$userid." and userid=".$this->userInfo['id'])){
					if($dao->delete("postid=".$userid)){
						if(isset($_GET["ajax"])){
							echo "success";
						}else{
							$this->success(L("action_success"));
						}
					}else{
						if(isset($_GET["ajax"])){
							echo "error";
						}else{
							$this->error(L("action_error"));
						}
					}
				}else{
					if(isset($_GET["ajax"])){
							echo "error";
						}else{
							$this->error(L("no_record"));
						}
				}
			}

			public function notify(){
				if(isset($_GET["ajax"])){
					$list = D("notify")->findAll("userid=".$this->userInfo['id']." and isread=0","*","regtime desc");
					$this->assign("list",$list);
					$this->display("notify4Ajax");
				}else{
					$list = D("notify")->findAll("userid=".$this->userInfo['id'],"*","regtime desc");
					$this->assign("list",$list);
					$this->display("");
				}
			}

			public function delnotify(){
				$id = safeRequest($_GET["id"]);
				if(D("notify")->delete("id=".$id." and userid=".$this->userInfo['id'])){
					isset($_GET["ajax"]) ? print "success" : $this->success(L("delete_notify_success"));
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("delete_notify_error"));
				}
			}
		}
?>